package work.jame.blog.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import work.jame.blog.framework.limit.RequestLimit;
import work.jame.blog.framework.result.Result;
import work.jame.blog.service.FileService;

import java.util.concurrent.TimeUnit;

/**
 * @author : Jame
 * @date : 2023-05-17 14:33
 **/
@Api(tags = "文件")
@Controller
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    @RequestLimit(value = 40, timeUnit = TimeUnit.MINUTES)
    @ResponseBody
    @ApiOperation("上传文章内图片")
    @PostMapping("/uploadArticleImg")
    public Result<?> uploadArticleImg(@RequestPart("img") MultipartFile img) {
        String fileName = fileService.uploadArticleImg(img);
        if (StringUtils.hasText(fileName)) {
            return Result.succeed(fileName);
        }
        return Result.fail();
    }


    @RequestLimit(value = 10, timeUnit = TimeUnit.MINUTES)
    @ResponseBody
    @ApiOperation("上传用户头像图片")
    @PostMapping("/uploadUserAvatar")
    public Result<?> uploadUserAvatar(@RequestPart("img") MultipartFile img) {
        String fileName = fileService.uploadUserAvatar(img);
        if (StringUtils.hasText(fileName)) {
            return Result.succeed(fileName);
        }
        return Result.fail();
    }


    @ApiOperation("获取文章内的图片")
    @GetMapping("/article/img")
    public void getArticleImage(@RequestParam("fileName") String fileName) {
        fileService.getArticleImg(fileName);
    }


    @ApiOperation("获取用户头像")
    @GetMapping("/user/avatar")
    public void getUserAvatar(@RequestParam("fileName") String fileName) {
        fileService.getUserAvatar(fileName);

    }


}
